package com.demo.auth.config;

import com.demo.auth.enhan.MyJwtTokenEnhancer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * jwt token 配置类
 *
 * @author YT
 * @date 2019-07-18
 */
@Configuration
public class JwtTokenConfig {

    /**
     * 使用 jwtTokenStore 存储 token
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 生成 jwt
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 生成签名 key
        jwtAccessTokenConverter.setSigningKey("yt");
        return jwtAccessTokenConverter;
    }

    /**
     * 用于扩展 jwt
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public TokenEnhancer tokenEnhancer() {
        return new MyJwtTokenEnhancer();
    }
}
